package org.jsola.hr.common;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jsola.common.ArrayKit;
import org.jsola.common.BeanKit;
import org.jsola.common.StrKit;
import org.jsola.hr.provider.IUserProviderService;
import org.jsola.hr.service.IChannelService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.ChannelVO;
import org.jsola.hr.vo.HrNoticeUserIdJsonVO;
import org.jsola.hr.vo.MyEmpInfoVO;
import org.jsola.notice.constant.EmpType;
import org.jsola.notice.vo.NoticeRuleSettingVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.entity.UserDO;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wcl
 */
@Slf4j
@Component
public class NoticeKit {

    /**
     * 获取通知的userId
     */
    public static List<String> getNoticeUserId(String empId, NoticeRuleSettingVO ruleSettingVO, TokenUser tokenUser) {

        List<String> userIdList = new ArrayList<>();
        String empType = ruleSettingVO.getEmpType();
        List<String> empTypeList = ArrayKit.split(empType, ",");
        if (CollectionUtils.isEmpty(empTypeList)) {
            log.info("发送失败：通知人员类型为空, code: {},接收人员类型: {}, 用工单位id: {}", ruleSettingVO.getNoticeTypeCode(),
                    empType, ruleSettingVO.getCompanyId());
            return new ArrayList<>();
        }
        // 添加自定义
        if (empTypeList.contains(EmpType.CUSTOM.getValue())) {
            HrNoticeUserIdJsonVO hrNoticeUserIdJsonVO = JSONObject.parseObject(ruleSettingVO.getUserIdParam(), HrNoticeUserIdJsonVO.class);
            if (hrNoticeUserIdJsonVO != null) {
                userIdList.addAll(ListKit.stringToList(hrNoticeUserIdJsonVO.getUserIds()));
                userIdList.addAll(ListKit.stringToList(hrNoticeUserIdJsonVO.getChannelUserIds()));
            }
        }
        // 添加管理员
        if (empTypeList.contains(EmpType.ADMIN.getValue())) {
            IUserProviderService userProviderService = BeanKit.getBean(IUserProviderService.class);
            userIdList.addAll(userProviderService.selectUserIdHasRoleInCompany(ruleSettingVO.getCompanyId(), tokenUser));
        }
        // 添加发起者本人
        if (empTypeList.contains(EmpType.SELF.getValue())) {
            IEmpInfoService empInfoService = BeanKit.getBean(IEmpInfoService.class);
            MyEmpInfoVO myEmpInfoVO = empInfoService.selectByEmpId(empId, tokenUser);
            userIdList.add(myEmpInfoVO.getUserId());
        }
        // 添加渠道发送人员
        if (empTypeList.contains(EmpType.CHANNEL.getValue())) {
            if (StrKit.isNotEmpty(empId)) {
                IChannelService channelService = BeanKit.getBean(IChannelService.class);
                ChannelVO channelVO = channelService.selectByEmpId(empId, tokenUser.getSiteId());
                if (channelVO != null) {
                    IUserProviderService userProviderService = BeanKit.getBean(IUserProviderService.class);
                    UserDO userDO = userProviderService.selectByPhone(channelVO.getPhone());
                    if (userDO != null) {
                        userIdList.add(userDO.getId());
                    }
                }
            }
        }
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>();
        }
        // 去重
        return userIdList.stream().distinct().collect(Collectors.toList());
    }
}
